package com.dingreading.cloud.mall.service.impl;

import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.mall.entity.CooperPayOrder;
import com.dingreading.cloud.mall.entity.table.CooperPayOrderTableDef;
import com.dingreading.cloud.mall.mapper.CooperPayOrderMapper;
import com.dingreading.cloud.mall.service.CooperPayOrderService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CooperPayOrderServiceImpl extends BaseServiceImpl<CooperPayOrderMapper, CooperPayOrder, CooperPayOrderTableDef> implements CooperPayOrderService {

    @Override
    protected CooperPayOrderTableDef getTable() {
        return CooperPayOrderTableDef.cooperPayOrder;
    }

    @Override
    public Page<CooperPayOrder> pageList(PageUtil pageUtil, String keyword, Integer status, Integer payType) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.userName.like(keyword)
                    .or(table.userPhone.like(keyword))
                    .or(table.saleName.like(keyword))
                    .or(table.code.like(keyword))
                    .or(table.orderUid.like(keyword))
            );
        }
        if (null != status)
            condition.and(table.status.eq(status));
        if (null != payType)
            condition.and(table.payType.eq(payType));

        QueryWrapper wrapper = QueryWrapper.create().where(condition).orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public CooperPayOrder getByCode(String code) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public boolean payOk(String code, Integer payType, String tradeType, String payId, String tradeNo, String payTime) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code));
        condition.and(table.status.eq(2));
        return UpdateChain.of(CooperPayOrder.class)
                .set(table.payType, payType)
                .set(table.tradeType, tradeType)
                .set(table.payTime, payTime)
                .set(table.buyerId, payId)
                .set(table.tradeNo, tradeNo)
                .set(table.status, 1)
                .where(condition)
                .update();
    }

}
